package com.shujia.flink.sql

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.table.api.EnvironmentSettings
import org.apache.flink.table.api.bridge.scala.StreamTableEnvironment

object Demo7ReadHbase {
  def main(args: Array[String]): Unit = {
    //创建flink的环境
    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val bsSettings: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner() //使用blink计划器
      .inStreamingMode() //流模式
      .build()

    /**
      * 创建flink sql 环境
      *
      */

    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)

    /**
      * 读取hbase 中的数据--- 有界流
      *
      */
    bsTableEnv.executeSql(

      """
        |CREATE TABLE hbase_student (
        | id STRING,
        | info ROW<name STRING , age INT , gender STRING , clazz STRING>,
        | PRIMARY KEY (id) NOT ENFORCED
        |) WITH (
        | 'connector' = 'hbase-1.4',
        | 'table-name' = 'student',
        | 'zookeeper.quorum' = 'master:2181'
        |)
        |
      """.stripMargin)

    bsTableEnv.executeSql(
      """
        |CREATE TABLE print_table
        |(
        |clazz STRING,
        |num BIGINT
        |)
        |WITH ('connector' = 'print')
        |
      """.stripMargin)


    bsTableEnv.executeSql(
      """
        |
        |insert into print_table
        |select info.clazz as clazz,count(1) as num
        |from hbase_student
        |group by info.clazz
        |
      """.stripMargin)



  }

}
